<?php

function render_form_field($field_form_name, $field_description, $field_value = "") {
    echo "<div class=\"ui-widget\">";
    echo "<label for=\"".$field_form_name."\">".$field_description."</label>";
    echo "<input class=\"TextField ui-corner-all\" id=\"".$field_form_name."\" name=\"".$field_form_name."\" type=\"text\" value=\"".$field_value."\" />";
    echo "</div>";
}

/**
 *
 * @return array|bool 
 */
function get_field_types() {
    $func_name = 'get_field_types';
    log_message('[' . $func_name . ']' . LOG_PING);
    $db = @new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
    if (mysqli_connect_errno()) {
        log_error("[" . $func_name . "] " . ERROR_DB_CONNECT . mysqli_connect_error() . '(' . mysqli_connect_errno() . ')');
    }

    $sql = "SELECT * FROM `input_fields`";

    $result = $db->query($sql);
    unset($sql);

    if (!$result) {
        log_error("[" . $func_name . "] " . ERROR_DB_QUERY . $db->error);
    }
    unset($db);
    $return = array();
    while ($row = $result->fetch_assoc()) {
        $return[] = $row;
    }
    log_message('[' . $func_name . ']' . RETURN_VALUE . $return);
    unset($result, $func_name);
    return $return;
}

/**
 *
 * @param string $type Gesuchter Feld Typ
 * @return string Tabellen Name 
 */
function get_field_table($type) {
    $func_name = 'get_field_table';
    log_message('[' . $func_name . ']' . LOG_PING);
    $db = @new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
    if (mysqli_connect_errno()) {
        log_error("[" . $func_name . "] " . ERROR_DB_CONNECT . mysqli_connect_error() . '(' . mysqli_connect_errno() . ')');
    }

    $sql = "SELECT * 
            FROM `input_fields` 
            WHERE `field_name` LIKE CONVERT( _utf8 '" . $type . "'
            USING latin1 ) 
            COLLATE latin1_swedish_ci
            LIMIT 1";

    $result = $db->query($sql);
    unset($sql);

    if (!$result) {
        log_error("[" . $func_name . "] " . ERROR_DB_QUERY . $db->error);
    }
    unset($db);
    $return = array();
    while ($row = $result->fetch_assoc()) {
        $return = $row['field_table'];
    }
    log_message('[' . $func_name . ']' . RETURN_VALUE . $return);
    unset($result, $func_name);
    return $return;
}

/**
 * @name find_field_input()
 * Marken Suchen
 *
 * Gibt alle Werte zu eine Feld Eingabe. Im Fehlerfall wird FALSE zurück gegeben;
 *
 * @param string $type      Feld Typ
 * @param string $input     Gesuchter Wert  
 * @return array|bool      Array für Manufakturen imf fehlerfall false
 */
function find_field_input($type, $input) {
    $func_name = 'find_field_input';

    if ((get_field_table($type)) && ($input != "")) {

        $db = @new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
        if (mysqli_connect_errno()) {
            log_error("[" . $func_name . "] " . ERROR_DB_CONNECT . mysqli_connect_error() . '(' . mysqli_connect_errno() . ')');
        }

        $sql = "SELECT * FROM `" . get_field_table($type) . "` WHERE `field_value` LIKE '%" . $input . "%'";

        $result = $db->query($sql);
        if (!$result) {
            log_error("[" . $func_name . "] " . ERROR_DB_QUERY . $db->error . $sql);
        }
        unset($sql);
        unset($db);


        $return = array();
        while ($row = $result->fetch_assoc()) {
            $return[] = $row;
        }

        unset($result, $func_name);
        return $return;
    } else
        return false;
}

/**
 * Marke Hinzufügen
 *
 * Die Funktion läd ein Übergebenes Porzellan Objekt in die 
 * Datenbank und gibt die Objekt ID zurück.
 *
 * @param string $type      Feld Typ
 * @param string $value     Feld Wert
 * @return int              Gibt die Marken ID oder 0 zurück
 * 
 * 
 * 
 */
function add_field_input($type, $value) {
    $func_name = "add_field_input";
    log_message('[' . $func_name . ']' . LOG_PING);
    log_message('[' . $func_name . ']' . SEARCH_FOR . "$value / $type");
    $search_result = find_field_input($type, $value);
    log_message('[' . $func_name . ']' . SEARCH_RESULTS . count($search_result));


    if (count($search_result) == 1) {
        log_message('[' . $func_name . ']' . SEARCH_OK . $search_result[0]['field_id']);
        return $search_result[0]['field_id'];
    } else {
        log_message('[' . $func_name . ']' . SEARCH_FAIL);

        $db = @new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
        if (mysqli_connect_errno()) {
            log_error("[" . $func_name . "] " . ERROR_DB_CONNECT . mysqli_connect_error() . '(' . mysqli_connect_errno() . ')');
        }
        $sql = "INSERT INTO `" . get_field_table($type) . "` (
                                    `field_value`
                            )
                            VALUES (
                                    '" . $value . "'
                            );";

        $result = $db->query($sql);
        unset($sql);

        if (!$result) {
            log_error("[" . $func_name . "] " . ERROR_DB_QUERY . $db->error);
        }
        if ($result == false) {
            var_dump($result);
            unset($result);
            return 0;
        } else {
            unset($result);
            return $db->insert_id;
        }
    }
}

/**
 * @name get_field_input($type, $id)
 * Gibt den Inhalt eines Feldes vom übergebenen Typ mit der übergebenen ID zurück
 * @param string $type FeldType
 * @param int $id FeldID
 * @return string|bool Feld Inhalt oder false
 */
function get_field_input($type, $id) {
    $func_name = 'get_field_input';
    if ($id == 0) {
        return ANSWER_NA;
    } else {

        if ((get_field_table($type)) && ($id != "") && ($id != NULL)) {

            $db = @new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
            if (mysqli_connect_errno()) {
                log_error("[" . $func_name . "] " . ERROR_DB_CONNECT . mysqli_connect_error() . '(' . mysqli_connect_errno() . ')');
            }


            $sql = "SELECT *
                    FROM `" . get_field_table($type) . "`
                    WHERE `field_id` = " . $id . "
                    LIMIT 1;";


            //$sql = "SELECT * FROM `" . get_field_table($type) . "` WHERE `field_value` LIKE '%" . $input . "%'";

            $result = $db->query($sql);
            if (!$result) {
                log_error("[" . $func_name . "] " . ERROR_DB_QUERY . $db->error . $sql);
            }
            unset($sql);
            unset($db);


            $row = $result->fetch_assoc();
            unset($result, $func_name);
            return $row['field_value'];
        } else
            return false;

        //return "Feld: $type Nummer: $id";
    }
}

?>
